package com.gxuwz.com_property.menu.controller;

import com.gxuwz.com_property.common.dto.ResponseDto;
import com.gxuwz.com_property.login.pojo.LoginUser;
import com.gxuwz.com_property.menu.pojo.Menu;
import com.gxuwz.com_property.menu.service.MenuService;
import com.gxuwz.com_property.utils.JwtUtil;
import com.gxuwz.com_property.utils.RedisUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class MenuController {

    /**
     * redis jwt 工具类注入
     * */
    @Autowired
    public JwtUtil jwtUtil;

    @Autowired
    public RedisUtil redisUtil;

    @Autowired
    private MenuService menuService;

    @Autowired
    private Menu menu;

    /**
     * 查询菜单列表
     * */
    @PostMapping ("/menus")
    public ResponseDto findMenu(@RequestBody() Menu menu){


        List<Menu> menus = menuService.SelectMenu(menu);
        if (menus!=null){
            return ResponseDto.success("查询菜单成功",menus);
        }else {
            return ResponseDto.error("查询菜单失败");
        }
    }
    /**
     * 根据登录用户的token，获取对应的权限，返回不同的菜单列表
     * */
    @PostMapping("/menu/byRole")
    public ResponseDto findMenuByToken(@RequestBody LoginUser loginUser){
        String token = loginUser.getToken();
        System.out.println("token:"+loginUser.getToken());

        /**
         * token 有效，并且token不为空
         *
         * 从redis查询是否存在这个token boolean tokenExistsInRedis = redisUtil.hasKey(token);
         *
         * */

        // 检查 Redis 中是否存在该令牌
        boolean tokenExistsInRedis = redisUtil.hasKey(token);

        // 如果 Redis 中不存在令牌，则返回 HTTP 401 Unauthorized 错误
        if (tokenExistsInRedis) {
            /**
             * 验证token
             * */
            if (jwtUtil.validateToken(token)){
                Claims claims = jwtUtil.extractClaims(token);

                String username = claims.getSubject();
                System.out.println("findMenu:"+username);
                String role = (String) claims.get("role");
                System.out.println("findMenu:"+role);
                menu.setRoleId(role);
                System.out.println("用户权限是role："+menu.getRoleId());

                List<Menu> menuByToken = menuService.findMenuByToken(menu);
                System.out.println("menuByToken:"+menuByToken);
                if (menuByToken!=null){
                    return ResponseDto.success("查询菜单成功",menuByToken);
                }else {
                    return ResponseDto.error("查询菜单失败");
                }
            }
        }
            /**
             * 演示：未从redis获取，校验token是否存在
             * */
/*            if (token!=null && jwtUtil.validateToken(token)){

            Claims claims = jwtUtil.extractClaims(token);

            String username = claims.getSubject();
            System.out.println("findMenu:"+username);
            String role = (String) claims.get("role");
            System.out.println("findMenu:"+role);

            *//**
             * 将权限赋值给menu菜单的实体类中
             * *//*
            menu.setRoleId(role);
                System.out.println("用户权限是role："+menu.getRoleId());

                List<Menu> menuByToken = menuService.findMenuByToken(menu);

                if (menuByToken!=null){
                    return ResponseDto.success("查询菜单成功",menuByToken);
                }else {
                    return ResponseDto.error("查询菜单失败");
                }
        }*/

        return ResponseDto.error("Token无效");
    }

    /**
     * 添加菜单
     * */
    @PostMapping("/addMenu")
    public ResponseDto addMenu(@RequestBody Menu menu){

        boolean addmenuboolean = menuService.addMenu(menu);
        if(addmenuboolean){
            return ResponseDto.success("添加菜单成功");
        }else {
            return ResponseDto.error("添加菜单失败");
        }

    }
    /**
     * 修改菜单
     * */
    @PostMapping
    public ResponseDto updateMenu(@RequestBody Menu menu){
        boolean updateMenuboolean = menuService.updateMenu(menu);
        if(updateMenuboolean){
            return ResponseDto.success("修改菜单成功");
        }else {
            return ResponseDto.error("修改菜单失败");
        }
    }

    /**
     * 删除菜单
     * */
    @PostMapping("/delectMenu")
    public ResponseDto delectMenu(@RequestBody int id){
        boolean delectMenuboolean = menuService.delectMenu(id);
        if(delectMenuboolean){
            return ResponseDto.success("删除菜单成功");
        }else {
            return ResponseDto.error("删除菜单失败");
        }
    }

}
